package com.kj.xuyuan.http;

import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;

/**
 * Created by guchunyan on 2018/7/10.
 */

public class GsonUtil {
    public static JsonParser jsonParser = new JsonParser();
    private static Gson gson = new Gson();
    static {
        if (gson == null) {
            gson = new Gson();
        }
    }

    public static String toJson(Object obj) {
        return gson.toJson(obj);
    }

    public static <T> T fromJson(String str, Type type) {
        return gson.fromJson(str, type);
    }

    public static <T> T fromJson(String str, Class<T> type) {
        return gson.fromJson(str, type);
    }

    public static Map<String, String> jsonToMapStr(String json){
        return gson.fromJson(json,new TypeToken<Map<String, String>>(){}.getType());
    }

    public static Map<String, Object> jsonToMapObj(String json){
        return gson.fromJson(json,new TypeToken<Map<String, Object>>(){}.getType());
    }
}
